package com.come4loves.game.dataconfig.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.come4loves.core.dataconfig.ServiceAdapter;
import com.come4loves.game.dataconfig.model.PlayerDeckConfig;
import com.come4loves.game.module.card.constant.CardType;
import com.come4loves.game.module.card.model.CardGroupModel;
import com.come4loves.game.module.card.model.CardModel;
import com.come4loves.game.module.fight.model.FightCard;

/**
 * zhuangbei卡
 * 
 * @author jerry
 *
 */
@Component
public class PlayerDeckService extends ServiceAdapter {

	private static Map<Integer, PlayerDeckConfig> deckMap = new HashMap<Integer, PlayerDeckConfig>();

	@Override
	public void clear() {
		deckMap.clear();
	}

	@Override
	public void initialize() {
		List<PlayerDeckConfig> list = this.dataConfig.listAll(this, PlayerDeckConfig.class);
		for (PlayerDeckConfig config : list) {
			deckMap.put(config.getId(), config);
		}
	}

	public static PlayerDeckConfig get(int id) {
		return deckMap.get(id);
	}

	public static boolean exsit(int goodsId) {
		return deckMap.containsKey(goodsId);
	}
	
	/**
	 * 获得初始化卡片租
	 * @param isMainCard 是否需要主将卡
	 * @return
	 */
	public static List<FightCard> getDefaultFightCard(boolean isMainCard) {
		List<FightCard> cardList = new ArrayList<FightCard>();
		for (PlayerDeckConfig config : deckMap.values()) {
			FightCard fightCard = null;
			if (isMainCard) {
				if (config.geCardType() == CardType.GENERAL) {
					fightCard = FightCard.valueOf(config.geCardType(), config.getCardid());
					cardList.add(fightCard);
				}
			} else {
				if (config.geCardType() != CardType.GENERAL) {
					fightCard = FightCard.valueOf(config.geCardType(), config.getCardid());
					cardList.add(fightCard);
				}
			}
		}
		return cardList;
	}
	
	public static CardGroupModel getDefaultGroupModel() {
		return getCardGroups(true);
	}
	/**
	 * 获得初始化卡片租
	 * @param isMainCard 是否需要主将卡
	 * @return
	 */
	public static CardGroupModel getCardGroups(boolean isMainCard) {
		CardGroupModel defaultGroupModel = new CardGroupModel();
		for (PlayerDeckConfig config : deckMap.values()) {
			if (isMainCard == false) {
				if (config.geCardType() == CardType.GENERAL) {
					continue;
				}
			}
			int level = SystemService.systemConfig.getCardLevelInitialValue();
			int priority = SystemService.systemConfig.getCardPriorityInitialValue();
			CardType cardType = CardType.geCardType(config.getCardtype());
			int cardId = config.getCardid();
			CardModel model = CardModel.valueOf(cardId, level, cardType, 1, priority);
			defaultGroupModel.groupId = 1;
			defaultGroupModel.groupFlag = 1;
			defaultGroupModel.name = "默认牌组不要BB";
			defaultGroupModel.cardList.add(model);
		}
		return defaultGroupModel;
	}
}
